package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commmon.CustomException;
import org.example.commmon.R;
import org.example.dto.CombinationDto;
import org.example.mapper.SetmealMapper;
import org.example.pojo.Category;
import org.example.pojo.Combination;
import org.example.pojo.CombinationMedicine;
import org.example.service.CategoryService;
import org.example.service.CombinationMedicineService;
import org.example.service.CombinationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;



@Transactional
@Service
public class CombinationServiceImpl extends ServiceImpl<SetmealMapper, Combination> implements CombinationService {


    @Autowired
    private CombinationMedicineService combinationMedicineService;

    @Autowired
    private CombinationService combinationService;

    @Autowired
    private CategoryService categoryService;
    @Override
    public void saveWithMedicine(CombinationDto combinationDto) {
        //保存组合表
        this.save(combinationDto);
        System.out.println("--------------------------------------");



        //获得组合的药品列表（含有药品的名称，id，价格，数量）
        List<CombinationMedicine> combinationMedicines = combinationDto.getCombinationMedicines();
        //但是不包含套菜id
        Long id = combinationDto.getId();
        for (int i = 0; i < combinationMedicines.size(); i++) {
            combinationMedicines.get(i).setCombinationId(id);
        }
        System.out.println("--------------------------------------");
        boolean b = combinationMedicineService.saveBatch(combinationMedicines);
        System.out.println(b);

    }


    @Transactional
    @Override
    public void deleteWithMedicine(List<Long> ids) {




        //先删除combinationMedicine 根据组合id删除
        for (int i = 0; i < ids.size(); i++) {
            Long combinationId=ids.get(i);
            Combination combination = this.getById(combinationId);

            //停售状态下才可以删除
            if (combination.getStatus()==0){
            LambdaQueryWrapper<CombinationMedicine> combinationMedicineLambdaQueryWrapper = new LambdaQueryWrapper<>();
            combinationMedicineLambdaQueryWrapper.eq(org.example.pojo.CombinationMedicine::getCombinationId,combinationId);
            combinationMedicineService.remove(combinationMedicineLambdaQueryWrapper);
            //再删除组合表
            this.removeById(combinationId);
            }
            else {
               throw  new CustomException("正在售卖中，不能删除");
            }
        }




    }

    @Override
    public CombinationDto getDtoById(Long id) {

        //根据id获得一个组合对象
        Combination combination = this.getById(id);
//        创建dto对象并拷贝
        CombinationDto combinationDto = new CombinationDto();
        BeanUtils.copyProperties(combination,combinationDto);
//        获取组合药品对象列表
        LambdaQueryWrapper<CombinationMedicine> combinationMedicineLambdaQueryWrapper = new LambdaQueryWrapper<>();
        combinationMedicineLambdaQueryWrapper.eq(org.example.pojo.CombinationMedicine::getCombinationId,id);
        List<CombinationMedicine> list = combinationMedicineService.list(combinationMedicineLambdaQueryWrapper);
        //将列表复制给dto对象
        combinationDto.setCombinationMedicines(list);
        //根据分类id获取分类名称
        Category byId = categoryService.getById(combination.getCategoryId());
        combinationDto.setCategoryName(byId.getName());

        return combinationDto;
    }

    @Transactional
    @Override
    public void updateWithCombinationMedicine(CombinationDto combinationDto) {
        //对combinationMedicine
        //1.删除原来的数据
        LambdaQueryWrapper<CombinationMedicine> combinationMedicineLambdaQueryWrapper = new LambdaQueryWrapper<>();
        combinationMedicineLambdaQueryWrapper.eq(org.example.pojo.CombinationMedicine::getCombinationId,combinationDto.getId());
        combinationMedicineService.remove(combinationMedicineLambdaQueryWrapper);
        List<CombinationMedicine> combinationMedicines = combinationDto.getCombinationMedicines();
        //将数据加上之前还要为组合药品设置组合id
        List<CombinationMedicine> combinationMedicineList = combinationMedicines.stream().map(item -> {
            item.setCombinationId(combinationDto.getId());
            return item;

        }).collect(Collectors.toList());
        //2.将数据加上
        combinationMedicineService.saveBatch(combinationMedicineList);

        //对combination

        this.updateById(combinationDto);

    }

    @Override
    public R<Page> getPage(int page, int pageSize, String name) {
        //创建分页器
        Page<Combination> combinationPage = new Page<>(page,pageSize);

        //构建条件查询器
        LambdaQueryWrapper<Combination> combinationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果name不为空 添加查询条件
        if (name!=null) combinationLambdaQueryWrapper.like(Combination::getName,name);

        //开始查询combination
        combinationService.page(combinationPage,combinationLambdaQueryWrapper);

        //此时查询出数据，但是combination表不含组合分类名称字段 只有分类id
        //所以需要联合分类id进行查询
        //封装为SetmealDto
        Page<CombinationDto> combinationDtoPage = new Page<>();
        //将combinationPage的数据给combinationDtoPage 同时忽略records
        BeanUtils.copyProperties(combinationPage,combinationDtoPage,"records");
        //拿到含有分类id的页面数据
        List<Combination> records = combinationPage.getRecords();
        //将每条数据的分类id取出 并查询出各自的分类名
        //将分类名复制给combinationDto
        List<CombinationDto> list = records.stream().map(item -> {
            CombinationDto combinationDto = new CombinationDto();
            Long categoryId = item.getCategoryId();
            BeanUtils.copyProperties(item, combinationDto);
            LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            categoryLambdaQueryWrapper.eq(Category::getId, categoryId);
            Category one = categoryService.getOne(categoryLambdaQueryWrapper);
            combinationDto.setCategoryName(one.getName());
            return combinationDto;


        }).collect(Collectors.toList());
        combinationDtoPage.setRecords(list);
        return R.success(combinationDtoPage);
    }


}
